<?php

namespace common\models;

use cm;
use yii;
use yii\base\Model;

class CmDataModel extends Model
{
    function upload($filestr,$type)
    {
        $dir = date('Ymd');

        $rootPath = BASE_PATH . 'upload/'. $filestr .'/' . $dir . '/';
        $rootPath = iconv("UTF-8", "gb2312", $rootPath);
        if (!is_dir($rootPath)) {
            mkdir($rootPath);
        }
        $fileType = $this->getFileType($_FILES['file']['tmp_name']);
        if($type == 'img' && !in_array($fileType, ['jpg','gif','bmp','png'])){
            Cm::$res->error('上传的图片格式不正确');
        }
        $time = explode(" ", microtime());
        $time = $time [1] . ($time [0] * 1000000);
        $time2 = explode(".", $time);
        $time = $time2 [0];

        $fileName = strtolower(md5($time)) . '.' . $fileType;

        $receiveFile = $rootPath . $fileName;
        move_uploaded_file($_FILES['file']['tmp_name'], $receiveFile);

        $path = "/upload/". $filestr ."/". $dir ."/" . $fileName;
        Cm::$res->susess(
            [
                'src' => BASE_URL . $path,
                "path" => $path,
                'name' => $_FILES['file']['name']
            ]
        );
    }

    function uploadTxt()
    {
        $rootPath = Yii::$app->params['wechat_token_file_path'];
        $fileName = $_FILES['file']['name'];

        $receiveFile = $rootPath . $fileName;
        move_uploaded_file($_FILES['file']['tmp_name'], $receiveFile);
        $path = "/" . $fileName;
        Cm::$res->susess(
            [
                'src' => BASE_URL . $path,
                "path" => $path,
                'name' => $fileName
            ]
        );
    }

    public function number($key = '', $formart = 'dHis', $randLen = 4)
    {
        $key = $key ? strtoupper($key) : '';
        list($msec, $second) = explode(' ', microtime());
        $date = date($formart, $second);
        $ms = sprintf("%'03.0f", floor($msec * 1000));

        $numbers = (string)mt_rand(10000000, 99999999);
        $max = (strlen($numbers) - (int)$randLen) - 1;
        $start = rand(0, $max);
        $result = substr($numbers, $start, $randLen);

        $number = $key . $date . $ms . $result;
        return $number;
    }

    public function getFileType($file)
    {
        $fp = fopen($file, "rb");
        $bin = fread($fp, 2); //只读2字节
        fclose($fp);
        $str_info = @unpack("C2chars", $bin);
        $type_code = intval($str_info['chars1'] . $str_info['chars2']);
        $file_type = '';
        switch ($type_code) {
            case 7790:
                $file_type = 'exe';
                break;
            case 7784:
                $file_type = 'midi';
                break;
            case 8075:
                $file_type = 'zip';
                break;
            case 8297:
                $file_type = 'rar';
                break;
            case 255216:
                $file_type = 'jpg';
                break;
            case 7173:
                $file_type = 'gif';
                break;
            case 6677:
                $file_type = 'bmp';
                break;
            case 13780:
                $file_type = 'png';
                break;
            default:
                $arr = explode('.', $_FILES['file']['name']);
                $file_type = $arr[count($arr) - 1];
                $file_type = empty($file_type) ? '' : $file_type;
                break;
        }

        return $file_type;
    }

    public function getSmsUsers($cid = 0)
    {
        $where = '';
        if ($cid)
            $where = " and community_id=$cid";
        $list = Cm::$db->all("select id,name,tel from wx_user where community_id>0 and state=1 {$where}");
        if (!$list)
            return [];

        foreach ($list as &$item) {
            $item['surname'] = mb_substr($item['name'], 0, 1, 'utf-8');
            $item['latter'] = $this->getFirstCharter($item['name']);
        }

        $sort = array(
            'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
            'field' => 'latter',       //排序字段
        );
        $arrSort = array();
        foreach ($list AS $uniqid => $row) {
            foreach ($row AS $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if ($sort['direction']) {
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $list);
        }
        return $list;
    }

    function getFirstCharter($str)
    {
        if (empty($str))
            return '#';
        $fchar = ord($str{0});
        if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
        try{
            $s1 = iconv('UTF-8', 'GBK', $str);
            $s2 = iconv('GBK', 'UTF-8', $s1);
            $s = $s2 == $str ? $s1 : $str;
            $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
            if ($asc >= -20319 && $asc <= -20284) return 'A';
            if ($asc >= -20283 && $asc <= -19776) return 'B';
            if ($asc >= -19775 && $asc <= -19219) return 'C';
            if ($asc >= -19218 && $asc <= -18711) return 'D';
            if ($asc >= -18710 && $asc <= -18527) return 'E';
            if ($asc >= -18526 && $asc <= -18240) return 'F';
            if ($asc >= -18239 && $asc <= -17923) return 'G';
            if ($asc >= -17922 && $asc <= -17418) return 'H';
            if ($asc >= -17417 && $asc <= -16475) return 'J';
            if ($asc >= -16474 && $asc <= -16213) return 'K';
            if ($asc >= -16212 && $asc <= -15641) return 'L';
            if ($asc >= -15640 && $asc <= -15166) return 'M';
            if ($asc >= -15165 && $asc <= -14923) return 'N';
            if ($asc >= -14922 && $asc <= -14915) return 'O';
            if ($asc >= -14914 && $asc <= -14631) return 'P';
            if ($asc >= -14630 && $asc <= -14150) return 'Q';
            if ($asc >= -14149 && $asc <= -14091) return 'R';
            if ($asc >= -14090 && $asc <= -13319) return 'S';
            if ($asc >= -13318 && $asc <= -12839) return 'T';
            if ($asc >= -12838 && $asc <= -12557) return 'W';
            if ($asc >= -12556 && $asc <= -11848) return 'X';
            if ($asc >= -11847 && $asc <= -11056) return 'Y';
            if ($asc >= -11055 && $asc <= -10247) return 'Z';
        }catch (\Exception $e){

        }
        return '#';
    }
}